package org.jeecg.modules.demo.mlwj.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.api.controller.ApiInfoController;
import org.jeecg.modules.demo.mlwj.entity.DaMlwj;
import org.jeecg.modules.demo.mlwj.mapper.DaMlwjMapper;
import org.jeecg.modules.demo.mlwj.service.IDaMlwjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import net.coobird.thumbnailator.Thumbnails;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static cn.hutool.core.util.StrUtil.str;

/**
 * @Description: 目录文件表
 * @Author: jeecg-boot
 * @Date:   2023-11-22
 * @Version: V1.0
 */
@Service
public class DaMlwjServiceImpl extends ServiceImpl<DaMlwjMapper, DaMlwj> implements IDaMlwjService {
    @Autowired
    private DaMlwjMapper DaMlwjMapper;
    @Value(value = "${jeecg.domainUrl.Dpuburl}")
    private String Dpuburl;
    @Value(value = "${jeecg.path.thumbnail}")
    private String thumbnail;
    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;
    @Autowired
    private ApiInfoController apiInfoController;
    public Page<DaMlwj> getmlwjlist(Page<DaMlwj>page , String ryid , String mlid, String wjmc){

        return   page.setRecords(DaMlwjMapper.getmlwjlist(page, ryid , mlid, wjmc));
    }
    public JSONObject addmlwj(String user,String orgcode,String id , JSONArray wjarray,String lx){
//        "uid" -> "vc-upload-1700727647742-36"
//        "lastModified" -> {Long@19418} 1596682923157
//        "lastModifiedDate" -> "2020-08-06T03:02:03.157Z"
//        "name" -> "1.jpg"
//        "size" -> {Integer@19424} 321257
//        "type" -> "image/jpeg"
//        "percent" -> {Integer@19428} 100
//        "originFileObj" -> {LinkedHashMap@19430}  size = 1
//        "status" -> "done"
//        "response" -> "http://192.168.2.7:8021/2023-11-23/17007295305111.jpg"
//        "xhr" -> {LinkedHashMap@19436}  size = 0
        JSONObject tmpRet = new JSONObject();
        try{
            for (int i = 0; i < wjarray.size(); i++) {
                if (lx.equals("upload")){
                    JSONObject student = wjarray.getJSONObject(i);
                    String wjmc = student.getString("name");

                    wjmc = wjmc.substring(0, wjmc.indexOf("."));
                    String wjurl = student.getString("response");
                    String sourcePath = wjurl.replace(Dpuburl,uploadpath);
                    String newPath = wjurl.replace(Dpuburl,thumbnail);
                    Thumbnails.of(sourcePath).scale(1f) //0-1 float 压缩大小
                            .outputQuality(0.7f) //0-1 压缩质量
                            .toFile(newPath);
                    String njurl = wjurl.replace(Dpuburl,Dpuburl+"thumbnail/");

                    File file = new File(sourcePath);
                    long fileSize = file.length();
                    DaMlwjMapper.mlwjadd(user, orgcode, id, wjmc,njurl,wjurl, String.valueOf(fileSize),newPath,sourcePath);
                }
                if (lx.equals("carmera")){
                    System.out.println("1");
                    String baseStr = wjarray.getString(i);
                    System.out.println("2");
                    String wjurl = apiInfoController.base64File(baseStr);
                    System.out.println("3");
                    String sourcePath = wjurl.replace(Dpuburl,uploadpath);
                    System.out.println("4");
                    String newPath = wjurl.replace(Dpuburl,thumbnail);
                    System.out.println("5");
                    Thumbnails.of(sourcePath).scale(1f) //0-1 float 压缩大小
                            .outputQuality(0.7f) //0-1 压缩质量
                            .toFile(newPath);
                    String njurl = wjurl.replace(Dpuburl,Dpuburl+"thumbnail/");
                    System.out.println("6");
                    File file = new File(wjurl);
                    System.out.println("7");
                    String wjmc = file.getName();
                    System.out.println("8");
                    wjmc = wjmc.substring(0, wjmc.indexOf("."));
                    System.out.println("9");
                    File sile = new File(sourcePath);
                    System.out.println("10");
                    long fileSize = sile.length();
                    DaMlwjMapper.mlwjadd(user,orgcode,id,wjmc,njurl,wjurl, String.valueOf(fileSize),newPath,sourcePath);
                }
            }
            tmpRet.put("code", "0");
        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return tmpRet;
    }
    public JSONObject editmlwj(String user, String orgcode, String id , String wjmc ,String sx){
        JSONObject tmpRet = new JSONObject();
        try{

            DaMlwjMapper.mlwjedit(user,orgcode,id,wjmc,sx);

            tmpRet.put("code", "0");
            tmpRet.put("msg", "修改完成");
        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return tmpRet;
    }
    public JSONObject delmlwj(String user, String orgcode, String id ){
        JSONObject tmpRet = new JSONObject();
        try{
            DaMlwjMapper.mlwjdel(user,orgcode,id);
            tmpRet.put("code", "0");
            tmpRet.put("msg", "删除完成");
        }
        catch(Exception ex){
            tmpRet.put("code", "1");
            tmpRet.put("msg", ex.toString());
        }
        return tmpRet;
    }
}
